<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" v-if="!listLoading">
      <el-row :gutter="20">
        <el-col :span="2">
          <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加课室</el-button>
        </el-col>
        <el-col :span="6">
          <el-input placeholder="请输入关键字" v-model="query.keyword">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="loadTable"></i>
          </el-input>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="课室名称"></el-table-column>
      <el-table-column prop="introduction" label="课室介绍"></el-table-column>
      <el-table-column prop="seat_num" label="座位数"></el-table-column>
      <el-table-column prop="seat_row" label="座位行数"></el-table-column>
      <el-table-column prop="seat_col" label="座位列数"></el-table-column>
      <!--<el-table-column prop="seat_info" label="座位信息"></el-table-column>-->
      <el-table-column prop="note" label="备注"></el-table-column>
      <!--<el-table-column prop="attachment" label="设备"></el-table-column>-->
      <el-table-column prop="is_disable" label="状态">
        <template slot-scope="scope">
          <a @click="able(scope.row.id)">
            <el-tag type='gray' v-if="scope.row.is_disable">已禁用</el-tag>
          </a>
          <a @click="disable(scope.row.id)">
            <el-tag type='success' v-if="!scope.row.is_disable">正常使用</el-tag>
          </a>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <template v-if="!listLoading">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="query.pagination.total"
          :page-sizes="[5, 10, 15, 20, 30]"
          :page-size="query.pagination.per_page"
          :current-page.sync="query.pagination.current_page"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </template>
    </div>
    <!--<edit-->
      <!--:editId="editId"-->
      <!--:dialogVisible="dialogVisible"-->
      <!--:title="title"-->
      <!--@closeDialog="closeDialog">-->
    <!--</edit>-->

  </div>
</template>

<script>
import edit from './edit'
import { GetTable, Delete, DisableClassroom, Able } from '@/api/classroom'

export default {
  components: {
    edit
  },
  data() {
    return {
      editId: null,
      visible: [],
      delVisible: false,
      dialogVisible: false,
      title: null,
      listLoading: false,
      query: {
        pagination: {
          total: 0,
          page: 1,
          per_page: 20
        },
        keyword: ''
      },
      tableData: [],
      is_disable: []
    }
  },
  created() {
    console.log('table created')
    this.loadTable()
  },
  methods: {
    loadTable() {
      this.listLoading = true
      GetTable(this.query.pagination.current_page, this.query.pagination.per_page, this.query.keyword, this.is_disable).then((res) => {
        this.listLoading = false
        this.tableData = res.data
        this.query.pagination = res.meta.pagination
      })
    },
    handleSizeChange(val) {
      this.query.pagination.per_page = val
      this.loadTable()
    },
    handleCurrentChange(val) {
      this.query.pagination.current_page = val
      this.loadTable()
    },
    handleCreate() {
      console.log('action: create')
      this.$router.push({ path: '/classroom/edit' })
      //     this.title = '添加'
      //        this.dialogVisible = true
    },
    handleEdit(row) {
      this.editId = row.id
      //  console.log(this.editId, 'idddd')
      this.$router.push({ path: '/classroom/edit', query: { id: this.editId }})
      //     this.title = '编辑'
      //        this.dialogVisible = true
    },
    handleDelete(row) {
      console.log('action: delete; row:', row)
      this.$confirm('此操作将删除一条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Delete(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadTable()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    closeDialog(loadTable) {
      this.dialogVisible = false
      if (loadTable) this.loadTable()
    },
    disable(id) {
      DisableClassroom(id).then((res) => {
        this.$message({
          message: '禁用成功',
          type: 'success'
        })
        this.loadTable()
        console.log(res.data.is_disable)
      })
    },
    able(id) {
      Able(id).then((res) => {
        this.$message({
          message: '启用成功',
          type: 'success'
        })
        this.loadTable()
        console.log(res.data.is_disable)
      })
    }
  }
}
</script>
